<template>
  <div class="tpl-container">
    <div class="mb20">
      <span class="color_494949 ft12 pr5">{{ translate('模板编码') }}：</span>
      <ElInput
        v-model="etCode"
        class="w164 mr14"
        :placeholder="translate('请输入模板编码')"
      />
      <span class="color_494949 ft12 pr5">{{ translate('模板名称') }}：</span>
      <ElInput
        v-model="etName"
        class="w164 mr14"
        :placeholder="translate('请输入模板名称')"
      />
      <ElButton class="settingBtn" type="primary" @click="search">
        {{ translate('查询') }}
      </ElButton>
    </div>
    <div class="add">
      <ElButton
        v-if="MenuAuth('add')"
        class="settingBtn"
        type="primary"
        @click="addFn"
        plain
      >
        {{ translate('新增') }}
      </ElButton>
    </div>
    <div class="table">
      <ElTable v-loading="loading" :border="true" :data="tableData">
        <ElTableColumn :label="translate('模板编号')" prop="ID" width="auto" />
        <ElTableColumn
          :label="translate('模板编码')"
          prop="ETCode"
          width="auto"
        />
        <ElTableColumn
          :label="translate('模板名称')"
          prop="ETName"
          width="auto"
        />
        <ElTableColumn
          :label="translate('宽度')"
          prop="ESLWidth"
          width="auto"
        />
        <ElTableColumn
          :label="translate('高度')"
          prop="ESLHeight"
          width="auto"
        />
        <ElTableColumn :label="translate('图片')">
          <template #default="{ row }">
            <div class="img-container">
              <ElImage
                fit="contain"
                :hide-on-click-modal="true"
                :initial-index="9"
                :preview-src-list="[row.Image]"
                :src="row.Image"
                :z-index="99999"
                :zoom-rate="1.2"
              />
            </div>
          </template>
        </ElTableColumn>
        <ElTableColumn :label="translate('预览图片')" prop="VerNo">
          <template #default="{ row }">
            <div>
              <el-image
                fit="contain"
                :initial-index="9"
                :preview-teleported="true"
                :src="row.PathImg + '?=' + timestamp"
                style="width: 60px; height: 60px"
                :style="
                  row.PathImg && row.LCDType === '05'
                    ? 'transform: rotate(-90deg)'
                    : ''
                "
                :zoom-rate="1.2"
                @click="priewshow(row)"
              />
            </div>
          </template>
        </ElTableColumn>
        <!-- <ElTableColumn label="备注" prop="Remark" width="auto" /> -->
        <ElTableColumn :label="translate('状态')">
          <template #default="{ row }">
            <div class="status">
              {{ row.Enabled == 1 ? translate('正常') : translate('未生效') }}
            </div>
          </template>
        </ElTableColumn>
        <ElTableColumn :label="translate('操作')" width="200">
          <template #default="{ row }">
            <ElButton
              v-if="MenuAuth('edit')"
              plain
              size="small"
              type="primary"
              @click="editFn(row)"
            >
              {{ translate('编辑') }}
            </ElButton>
            <ElButton
              v-if="MenuAuth('delete')"
              plain
              size="small"
              type="danger"
              @click="deleteFn(row)"
            >
              {{ translate('删除') }}
            </ElButton>
          </template>
        </ElTableColumn>
      </ElTable>
      <div v-if="total > 0" class="pagination">
        <ElPagination
          background
          :current-page="searchInfo.pageNo"
          :layout="layout"
          :page-size="searchInfo.pageSize"
          :total="total"
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
        />
      </div>
    </div>
    <priewImage v-if="showViewer" :form="priewlist" @close="closeFn" />
  </div>
</template>

<script lang="ts" setup>
  import { ref } from 'vue'
  import { MenuAuth } from '~/src/utils/permission'
  import { getPageData, deltemp } from '@/api/tag'
  import router from '@/router'
  import { useEleStore } from '@/store/modules/coms'
  import { translate } from '@/i18n'
  import priewImage from '@/views/eslstockList/components/priewImage'
  const etCode = ref('')
  const etName = ref('')
  const searchInfo = reactive({
    pageNo: 1,
    pageSize: 20,
    // total: 0,
    tableName: '',
    sort: '',
    order: '',
    wheres: `[{"name":"ETCode","value":${etCode.value},"displayType":"like"}, {"name":"ETName","value":${etName.value},"displayType":"like"}]`,
    export: false,
    value: '',
  })
  const loading = ref(false)
  const { clearWidgets, clearEditorData } = useEleStore()
  const addFn = async () => {
    await clearWidgets()
    await clearEditorData()
    router.push({ path: '/eslstock/editor' })
  }

  const layout = ref('total, sizes, prev, pager, next, jumper')
  const tableData = ref([])
  const total = ref(0)
  const priewlist = ref({})
  const showViewer = ref(false)
  // 获取表格数据
  const gettableData = async () => {
    loading.value = true
    const { data } = await getPageData(searchInfo)
    loading.value = false
    // console.log(data)
    tableData.value = data?.rows || []
    total.value = data?.total || 0
  }
  gettableData()
  //分页条数切换
  const handleSizeChange = (val: any) => {
    searchInfo.pageSize = val
    gettableData()
  }
  //分页切换
  const handleCurrentChange = (val: any) => {
    searchInfo.pageNo = val
    gettableData()
  }
  const priewshow = (row: any) => {
    row.PathBmp = row.PathImg
    priewlist.value = row
    showViewer.value = true
  }
  const closeFn = () => {
    showViewer.value = false
  }
  const search = () => {
    searchInfo.pageNo = 1
    if (etCode.value.trim() !== '' || etName.value.trim() !== '') {
      const arr = []
      etCode.value.trim() !== '' &&
        arr.push({ name: 'ETCode', value: etCode.value, displayType: 'like' })
      etName.value.trim() !== '' &&
        arr.push({ name: 'ETName', value: etName.value, displayType: 'like' })
      searchInfo.wheres = JSON.stringify(arr)
    } else {
      searchInfo.wheres = `[{"name":"ETCode","value": '',"displayType":"like"}, {"name":"ETName","value": '',"displayType":"like"}]`
    }
    gettableData()
  }
  const editFn = (row: any) => {
    router.push({ path: `/eslstock/editor`, query: { ID: row.ID } })
  }
  const $baseConfirm = inject('$baseConfirm') as any
  const deleteFn = async (row: any) => {
    $baseConfirm(translate('你确定要删除当前项吗'), null, async () => {
      await deltemp({
        id: row.ID,
      })
      gettableData()
    })
  }
</script>

<style lang="scss" scoped>
  .tpl-container {
    .color_494949 {
      color: #494949;
    }

    .noData {
      text-align: center;
      font-size: 16px;
      color: gray;
    }
    .w164 {
      width: 164px;
    }
    .mb20 {
      margin-bottom: 20px;
    }
    .mr14 {
      margin-right: 14px;
    }
    .add {
      margin-bottom: 10px;
      text-align: left;
    }
    .status {
      color: #e6a23c;
    }

    .img-container {
      display: flex;
      // justify-content: center;

      .img {
        width: 100px;
      }
    }
  }
  .el-table {
    & ::v-deep th.el-table__cell,
    ::v-deep td.el-table__cell {
      // 设置position 使得 子元素不与其产生新的层叠关系
      position: static;
    }
  }
</style>
